/********************************************************************************
*                 Copyright (C) 2011 - Las Venturas Playground                  *
*                      http://sa-mp.nl | http://lvp-iv.com                      *
*                                                                               *
*    This program is free software: you can redistribute it and/or modify       *
*    it under the terms of the GNU General Public License as published by       *
*    the Free Software Foundation, either version 3 of the License, or          *
*    (at your option) any later version.                                        *
*                                                                               *
*    This program is distributed in the hope that it will be useful,            *
*    but WITHOUT ANY WARRANTY; without even the implied warranty of             *
*    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the              *
*    GNU General Public License for more details.                               *
*                                                                               *
*    You should have received a copy of the GNU General Public License          *
*    along with this program.  If not, see <http://www.gnu.org/licenses/>.      *
*                                                                               *
*********************************************************************************
*                                                                               *
*  Las Venturas Playground - Core/Vehicle/functions.nut                         *
*                                                                               *
*  This file holds the Vehicle class.                                           *
*  @author Matthias <matthias@sa-mp.nl>                                         *
*                                                                               *
********************************************************************************/

/*
 * Class: Vehicle
 * 
 * This class holds all vehicle related functions.
 */
class Vehicle
{
    /**
     * This function gets the vehicle ID from a name or ID.
     * @author: Xanland <xanland@sa-mp.nl>
     *
     * @param vehicleName The string to fetch the ID from.
     */
    function getIDFromName (vehicleName)
    {
        if (isNumeric (vehicleName))
        {
            return vehicleName.tointeger ();
        }    
        
        for (local modelID = 0; modelID < aVehicleNames.len (); modelID++)
        {
            if (aVehicleNames [modelID].tolower ().find (vehicleName.tolower ()) != null)   return modelID;
        }
        return -1;
    }
    
    /**
     * This function loads all vehicles from the database.
     * @author: Matthias <matthias@sa-mp.nl>
     *
     */
    function loadVehicles()
    {
        // Create a new instance of the MySQL class.
        local Database = MySQL ();

        // Check if we still are connected.
        Database.checkConnection ();
        
        // Now select all vehicle data from the database.
        local Query = "SELECT * FROM vehicle_spawns";
        
        // Query the database
        local Result = Database.query (Query);
        
        // Initalize Rows.
        local Rows;
        // Fetch all the data.
        while (Rows = Database.fetch_assoc (Result))
        {
            // We set the variables.
            local vehicleLocX = Rows["x"].tofloat ();
            local vehicleLocY = Rows["y"].tofloat ();
            local vehicleLocZ = Rows["z"].tofloat ();
            local vehicleRotX = Rows["rotation_x"].tofloat ();
            local vehicleRotY = Rows["rotation_y"].tofloat ();
            local vehicleRotZ = Rows["rotation_z"].tofloat ();
            local vehicleModel = Rows["special_model"].tointeger ();
            local vehicleType = Rows["vehicle_type"].tointeger ();
            local vehicleColor1 = Rows["color_primary"].tointeger ();
            local vehicleColor2 = Rows["color_stripe"].tointeger ();
            local vehicleColor3 = Rows["color_rim"].tointeger ();
            local vehicleColor4 = Rows["color_extra"].tointeger ();
            
            // If the vehicleModel is -1, it should be random.
            if (vehicleModel != -1)
            {   
                // It's not, just spawn the vehicle with the correct model.
                aVehicles.append (createVehicle (vehicleModel, vehicleLocX, vehicleLocY, vehicleLocZ, vehicleRotX, vehicleRotY, vehicleRotZ, vehicleColor1, vehicleColor2, vehicleColor3, vehicleColor4));
            }
            else
            {
                // Initalize our random model.
                local randomModel = 0;
     
                // We now get a random index from the vehicles array, depending on which type it is.
                if (vehicleType == 0)
                {
					local arrayLength = vehiclesCheap.len ();
					local maxLength = (arrayLength - 1);
					local randomNumber = random (0, maxLength);
                    randomModel = vehiclesCheap[randomNumber];
                }
                else if (vehicleType == 1)
                {	
					local arrayLength = vehiclesNormal.len ();
					local maxLength = (arrayLength - 1);
					local randomNumber = random (0, maxLength);
                    randomModel = vehiclesNormal[randomNumber];
                }
                else if (vehicleType == 2)
                {
					local arrayLength = vehiclesSports.len ();
					local maxLength = (arrayLength - 1);
					local randomNumber = random (0, maxLength);
                    randomModel = vehiclesSports[randomNumber];
                }   
                else if (vehicleType == 3)
                {
					local arrayLength = vehiclesHelicopters.len ();
					local maxLength = (arrayLength - 1);
					local randomNumber = random (0, maxLength);
                    randomModel = vehiclesHelicopters[randomNumber];
                }   
                else if (vehicleType == 4)
				{
					local arrayLength = vehiclesBoats.len (); 
					local maxLength = (arrayLength - 1);
					local randomNumber = random (0, maxLength);
                    randomModel = vehiclesBoats[randomNumber];
                }
                else if (vehicleType == 5)
				{
					local arrayLength = vehiclesBikes.len (); 
					local maxLength = (arrayLength - 1);
					local randomNumber = random (0, maxLength);
                    randomModel = vehiclesBikes[randomNumber];
                }
                else if (vehicleType == 6)
				{
					local arrayLength = vehiclesIndustrial.len (); 
					local maxLength = (arrayLength - 1);
					local randomNumber = random (0, maxLength);
                    randomModel = vehiclesIndustrial[randomNumber];
                }
                else if (vehicleType == 7)
				{
					local arrayLength = vehiclesEmergency.len (); 
					local maxLength = (arrayLength - 1);
					local randomNumber = random (0, maxLength);
                    randomModel = vehiclesEmergency[randomNumber];
                }                  
                
                 // It's a random vehicle, we have a random model id, and random colors as well.
                 // Todo: vehicle type.
                aVehicles.append (createVehicle (randomModel, vehicleLocX, vehicleLocY, vehicleLocZ, vehicleRotX, vehicleRotY, vehicleRotZ, random (0, 123), random (0, 123), random (0, 123), random (0, 123)));
            }
        }
    }
    
    /**
     * This function saves the specified vehicle ID into the database.
     * @author: Matthias <matthias@sa-mp.nl>
     *
     * @param vehicleID The vehicle ID to save.
     */
    function saveVehicle (vehicleID, vehicleClass, isRandom)
    {
        // Get the vehicle's coordinates and colors.
        local vehicleCoords = getVehicleCoordinates (vehicleID);
        local vehicleColors = getVehicleColor (vehicleID);
        local vehicleRotation = getVehicleRotation (vehicleID);
        
        // We now set all the variables that will be inserted.
        local vehicleModel = getVehicleModel (vehicleID);
        local vehicleLocX = vehicleCoords[0];
        local vehicleLocY = vehicleCoords[1];
        local vehicleLocZ = vehicleCoords[2];
        local vehicleColor1 = vehicleColors[0];
        local vehicleColor2 = vehicleColors[1];
        local vehicleColor3 = vehicleColors[2];
        local vehicleColor4 = vehicleColors[3];
        local vehicleRotation_X = vehicleRotation[0];
        local vehicleRotation_Y = vehicleRotation[1];
        local vehicleRotation_Z = vehicleRotation[2];
        
        // Create a new instance of the MySQL class.
        local Database = MySQL ();

        // Check if we still are connected.
        Database.checkConnection ();
        
        // If the vehicle should be random, the model is is -1.
        local Query = "";
        
        if (isRandom == 1)
        {
            Query = @"INSERT INTO vehicle_spawns 
                                (x, y, z, rotation_x, rotation_y, rotation_z, vehicle_type, special_model, color_primary, color_stripe, color_rim, color_extra) 
                            VALUES 
                                ('" + vehicleLocX + @"', '" + vehicleLocY + @"', '" + vehicleLocZ + @"', '" + vehicleRotation_X + @"', '" + vehicleRotation_Y + @"', '" + vehicleRotation_Z + @"', '" + vehicleClass + @"',
                                '-1', '" + vehicleColor1 + @"', '" + vehicleColor2 + @"', '" + vehicleColor3 + @"', '" + vehicleColor4 + @"')
                           ";
        }
        else
        {
            Query = @"INSERT INTO vehicle_spawns 
                                (x, y, z, rotation_x, rotation_y, rotation_z, vehicle_type, special_model, color_primary, color_stripe, color_rim, color_extra) 
                            VALUES 
                                ('" + vehicleLocX + @"', '" + vehicleLocY + @"', '" + vehicleLocZ + @"', '" + vehicleRotation_X + @"', '" + vehicleRotation_Y + @"', '" + vehicleRotation_Z + @"', '" + vehicleClass + @"',
                                '" + vehicleModel + @"', '" + vehicleColor1 + @"', '" + vehicleColor2 + @"', '" + vehicleColor3 + @"', '" + vehicleColor4 + @"')
                           ";
        }
        
        // Query the database
        Database.query (Query);
    }
    
    /**
     * This function deletes all the vehicle from the database.
     * @author: Matthias <matthias@sa-mp.nl>
     *
     */
    function deleteVehicles ()
    {
        // Loop through all the vehicles and delete them.
        for(local i = 0; i < MAX_VEHICLES; i++) 
        {
            deleteVehicle (i);
        }
    }
    
    /**
     * This function returns true if the vehicle ID is valid, false otherwise.
     * @author: Matthias <matthias@sa-mp.nl>
     *
     */
    function isValidID (vehicleID)
    {
        if (vehicleID < 0 || vehicleID > 123 || vehicleID == 41 || vehicleID == 96 || vehicleID == 107 || vehicleID == 111) return false;
        
        return true;
    }
}